package com.bingxue.edu.management.system.controller;

import com.bingxue.edu.management.system.model.entity.Role;
import com.bingxue.edu.management.system.model.query.RoleQuery;
import com.bingxue.edu.management.system.model.req.RoleEmployeeReq;
import com.bingxue.edu.management.system.model.req.RoleReq;
import com.bingxue.edu.management.system.model.req.RoleResourceUpdateReq;
import com.bingxue.edu.management.system.model.resp.EmployeeResp;
import com.bingxue.edu.management.system.model.resp.RoleDetailResp;
import com.bingxue.edu.management.system.model.resp.RoleResp;
import com.bingxue.edu.management.system.service.RoleEmployeeService;
import com.bingxue.edu.management.system.service.RoleResourceService;
import com.bingxue.edu.management.system.service.RoleService;
import com.bingxue.edu.framework.common.model.PageQuery;
import com.bingxue.edu.framework.common.model.Pagination;
import com.bingxue.edu.framework.common.model.Response;
import com.bingxue.edu.framework.common.model.SortQuery;
import com.bingxue.edu.framework.mybatis.query.QueryBuilder;
import com.bingxue.edu.framework.security.ApiResource;
import com.mybatisflex.core.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 角色接口
 *
 * @author panbo
 * @since 2024/07/17 15:41
 */
@Tag(name = "角色接口")
@RestController
@RequestMapping("/system/role")
@RequiredArgsConstructor
public class RoleController {

    private final RoleService roleService;
    private final RoleResourceService roleResourceService;
    private final RoleEmployeeService roleEmployeeService;

    @Operation(summary = "查询列表")
    @ApiResource("system:org:manage")
    @GetMapping("/list")
    public Response<List<RoleResp>> list(RoleQuery query, SortQuery sortQuery) {
        QueryWrapper queryWrapper = QueryBuilder.build(query, sortQuery);
        List<RoleResp> list = roleService.listAs(queryWrapper, RoleResp.class);
        return Response.success(list);
    }

    @Operation(summary = "查询详情")
    @ApiResource("system:org:manage")
    @GetMapping("/detail/{id}")
    public Response<RoleDetailResp> detail(@PathVariable String id) {
        RoleDetailResp detail = roleService.getMapper().selectOneWithRelationsByIdAs(id, RoleDetailResp.class);
        return Response.success(detail);
    }

    @Operation(summary="创建数据")
    @ApiResource("system:org:manage")
    @PostMapping("/create")
    public Response<Void> create(@RequestBody @Validated RoleReq req) {
        roleService.createRole(req);
        return Response.success();
    }

    @Operation(summary="更新数据")
    @ApiResource("system:org:manage")
    @PostMapping("/update/{id}")
    public Response<Void> update(@RequestBody @Validated RoleReq req, @PathVariable String id) {
        Role role = roleService.copyById(req, id);
        roleService.updateById(role);
        return Response.success();
    }

    @Operation(summary="删除数据")
    @ApiResource("system:org:manage")
    @PostMapping("/delete/{ids}")
    public Response<Void> delete(@PathVariable List<String> ids) {
        roleService.removeByIds(ids);
        return Response.success();
    }

    @Operation(summary = "查询角色资源列表")
    @ApiResource("system:org:manage")
    @GetMapping("/resource/list")
    public Response<List<String>> getRoleResourceCodeList(@RequestParam String roleId) {
        return Response.success(roleResourceService.getResourceCodeListByRoleIds(List.of(roleId)));
    }

    @Operation(summary = "更新角色资源列表")
    @ApiResource("system:org:manage")
    @PostMapping("/resource/update")
    public Response<Void> updateRoleResource(@RequestBody @Validated RoleResourceUpdateReq req) {
        roleResourceService.updateRoleResource(req);
        return Response.success();
    }

    @Operation(summary = "根据角色查询员工列表")
    @ApiResource("system:org:manage")
    @GetMapping("/employee/page")
    public Response<Pagination<EmployeeResp>> getRoleEmployeePage(@RequestParam String roleId, @Validated PageQuery pageQuery) {
        return Response.success(roleEmployeeService.getEmployeePageByRoleId(roleId, pageQuery));
    }

    @Operation(summary = "移除员工角色")
    @ApiResource("system:org:manage")
    @PostMapping("/employee/remove")
    public Response<Void> removeRoleEmployee(@RequestBody @Validated RoleEmployeeReq req) {
        roleEmployeeService.removeRoleEmployee(req);
        return Response.success();
    }

    @Operation(summary = "添加员工角色")
    @ApiResource("system:org:manage")
    @PostMapping("/employee/create")
    public Response<Void> createRoleEmployee(@RequestBody @Validated RoleEmployeeReq req) {
        roleEmployeeService.createRoleEmployee(req);
        return Response.success();
    }

}
